<?php


namespace app\common\model;


use app\model\AdminAttendanceRecord;
use app\model\AdminAttendanceSys;
use app\model\AdminSchoolRkeRecord;
use app\model\AdminUnlockPasswordLog;
use app\model\AdminUnlockRecord;
use think\facade\Log;
use think\facade\Request;

class RecordCommon
{
    // 开锁记录写入
    public static function saveUnLockRecord($school_id, $dormitory_id, $student_dormitory_id, $staff_id, $staff_rule, $unlock_type = 0, $unlock_status = 1, $open_time = 0) {
        AdminUnlockRecord::create([
            'school_id' => $school_id,
            'dormitory_id' => $dormitory_id,
            'student_dormitory_id' => $student_dormitory_id,
            'staff_id' => $staff_id,
            'staff_rule' => $staff_rule,
            'unlock_type' => $unlock_type,
            'unlock_status' => $unlock_status,
            'request_ip' => sprintf("%u", ip2long(Request::ip())),
            'create_time' => $open_time > 0 ? $open_time : time(),
            'update_time' => $open_time > 0 ? $open_time : time(),
        ]);
    }

    // 获取开锁记录
    public static function getUnlockRecord($where = [], $start = 0, $limit = 15) {
        $list = AdminUnlockRecord::where($where)
            ->field('dormitory_id,student_dormitory_id,staff_id,unlock_type,create_time')
            ->with(['dormitory', 'student'])
            ->append(['unlock_type_text'])
            ->order('create_time', 'desc')
            ->paginate(['page' => $start, 'list_rows' => $limit])
            ->toArray();
        return ['total' => $list['total'], 'list' => $list['data']];
    }

    // 获取开闸记录
    public static function getRkeRecord($where = [], $start = 0, $limit = 15) {
        $list = AdminSchoolRkeRecord::where($where)
            ->order('srr_id', 'DESC')
            ->append(['acs_res_text','act_index_text','card_type_text'])
            ->paginate(['page' => $start, 'list_rows' => $limit])
            ->toArray();
        return ['total' => $list['total'], 'list' => $list['data']];
    }

    // 开锁密码生成记录
    public static function saveUnLockPassword($school_id, $dormitory_id, $student_dormitory_id, $staff_id, $type, $password) {
        AdminUnlockPasswordLog::create([
            'school_id' => $school_id,
            'dormitory_id' => $dormitory_id,
            'student_dormitory_id' => $student_dormitory_id,
            'staff_id' => $staff_id,
            'type' => $type,
            'password' => $password,
            'request_ip' => sprintf("%u", ip2long(Request::ip())),
            'create_time' => time(),
        ]);
    }

    // 考勤记录
    public static function setAttendanceRecord($data = [])
    {
        $data['clock_in_scope'] = $data['scope'];
        $data['date'] = date('Y-m-d');
        if (isset($data['dormitory_list']) && sizeof($data['dormitory_list']) > 0) {
            $data['clock_in_scope'] = AdminAttendanceSys::SCOPE_DORMITORY;
            $data['clock_in_deviceId'] = json_encode($data['dormitory_list'][0]);
        }
        if (isset($data['classroom_list']) &&sizeof($data['classroom_list']) > 0) {
            $data['clock_in_scope'] = AdminAttendanceSys::SCOPE_CLASSROOM;
            $data['clock_in_deviceId'] = json_encode($data['classroom_list'][0]);
        }
        try {
            AdminAttendanceRecord::create($data);
        } catch (\Exception $e) {
            Log::error("添加考勤记录异常:" . $e->getMessage());
        }
    }

    // 获取考勤记录
    public static function getAttendanceRecord($where = [], $start = 0, $limit = 15)
    {
        $list = AdminAttendanceRecord::where($where)
            ->field('attendance_id,staff_id,hour,clock_in_type,create_time,status')
            ->with(['attendance'])
            ->append(['clock_in_type_text','status_text'])
            ->order('create_time', 'desc')
            ->paginate(['page' => $start, 'list_rows' => $limit])
            ->toArray();
        return ['total' => $list['total'], 'list' => $list['data']];
    }
}